package thread.test;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author cyt
 * @create 2020-03-06 10:04
 */
public class Run implements Runnable {
    private Integer num = 0;

    private Lock lock = new ReentrantLock();

    public Run(Integer num) {
        this.num = num;
    }


    public static void main(String[] args) {
        for (int i = 0; i <= 3; i++) {
            Thread thread = new Thread(new Run(500), "A" + i);
            thread.start();
        }
    }
    @Override
    public void run() {
        while (true) {
            lock.lock();
            try {
                if (num < 100) {
                    break;
                }
                System.out.println(Thread.currentThread().getName() + "得到接力棒");
                for (int i = 0; i <= 100; i++) {
                    Thread.sleep(300);
                    System.out.println(Thread.currentThread().getName() + "跑了" + i + "米");
                }
                num -= 100;
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }
    }
}
